#include "PlPN.h"

SV* perl_pn;
PerlDocumentTracker g_document_tracker = PerlDocumentTracker();
PerlScriptTracker g_script_tracker = PerlScriptTracker();
PerlMenuTracker g_menu_tracker = PerlMenuTracker();
PerlAppEventSinkPtr g_app_event_sink = PerlAppEventSinkPtr(new PerlAppEventSink);
PerlRecorderPtr g_recorder = PerlRecorderPtr(new PerlRecorder);

void* get_app_event_sink() {
	return (void*)&g_app_event_sink;
}

// someone somewhere is defining THIS
// so I need to undo it so we can compile
#undef THIS

MODULE = PlPN          PACKAGE = PlPN

PROTOTYPES: ENABLE

INCLUDE: PN.xs
INCLUDE: Document.xs
INCLUDE: ScriptRegistry.xs
INCLUDE: TextOutput.xs
INCLUDE: SearchOptions.xs
INCLUDE: Options.xs
INCLUDE: AppEventSink.xs
INCLUDE: DocumentEventSink.xs
INCLUDE: TextEditorEventSink.xs
INCLUDE: ScriptRunner.xs
INCLUDE: Menu.xs
INCLUDE: Recorder.xs
INCLUDE: constants.xs

BOOT:
// remember - no blank lines allowed in BOOT section
my_pn = get_my_pn();
perl_pn = create_perl_object((void*)my_pn, "PlPN::PN");
g_document_tracker.Reset();
sv_setsv(
	get_sv("PlPN::PluginVersion", 1),	/* target; 1 means Perl will create if necessary*/
	eval_pv(PlPN_Plugin_Version, 1)	/* 1 means croak on error */
);

MODULE = PlPN          PACKAGE = PlPN

#
# POD for the following functions is in PlPN.pm
#

int
alert(class_or_object, msg, flags = 0, title = NULL)
	SV* class_or_object;
	char* msg;
	unsigned int flags;
	char* title;
	CODE:
		RETVAL = alert(flags, title, msg);
	OUTPUT:
		RETVAL

AV*
CurrentDocuments(class_or_object)
	SV* class_or_object;
	CODE:
		 RETVAL = g_document_tracker.PerlArray();
         sv_2mortal((SV*)RETVAL);
	OUTPUT:
		RETVAL

SV*
PN(...)
	CODE:
		RETVAL = perl_pn;
		SvREFCNT_inc(RETVAL);	// so it passes safely through the stack
	OUTPUT:
		RETVAL

void
report_refcount(class_or_object, perlvar, where)
	SV* class_or_object;
	SV* perlvar;
	char* where;
	CODE:
		report_refcount(perlvar, where);
